package com.isoftstone.controller;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import com.isoftstone.pojo.Educate;
import com.isoftstone.service.EducateService;
@Controller
public class EducateControlle {
	@Autowired
	private EducateService educateService;

	@RequestMapping("/addeducate")
	public String addeducate() {
		return "addeducate";
	}


	@RequestMapping("/modifyeducate")
	public String add4educate(Educate educate,HttpServletRequest req,HttpServletResponse resp){	
		String action = req.getParameter("action");
		int re = 0;
		if("deleteeducate".equals(action)) {
			String idsrc =  req.getParameter("id");
			int id = Integer.parseInt(idsrc);
			System.out.println("deleteid="+id);
			re = educateService.modifyeducate(id);
		}else if("updateeducate".equals(action)) {

			//System.out.println("deleteid="+id);
			re = educateService.updateeducate(educate);
		}else{
			educate.setCreatetime(new SimpleDateFormat("yyy-MM-dd HH:mm:ss").format(new Date()));
			re = educateService.register(educate);
		}
		if(re>0) {
			return list(req,resp);
		}
		return "error";
	}
	
	@RequestMapping("/educate")
	public String list(HttpServletRequest req,HttpServletResponse resp) {
		String educate = req.getParameter("educate");
		//String educate = (String)req.getAttribute("educate");
		List<Educate> educates = null;
		if("1".equals(educate)){
			//培训总结查看
			educates = educateService.getSummary();
		  }else{
			//培训计划查看
			  educates = educateService.getPlan();
		  }
		req.setAttribute("list", educates);
		return "listeducate";
	}
	//详细
	@RequestMapping("/detaileducate")
	public String detaileducate(HttpServletRequest req,HttpServletResponse resp) {
		String educates = req.getParameter("educate");
		String idsrc = req.getParameter("id");
		int id = Integer.parseInt(idsrc);
		Educate educate ;
		if("1".equals(educates)){
			//培训总结详细
			educate = educateService.detailSummaryeducate(id);
		  }else{
			educate = educateService.detailPlaneducate(id);
			//培训计划详细
		  }

		//System.out.println(educate.getName());
		req.setAttribute("educate", educate);
		return "detaileducate";
	}
	
	//培训完成
	@RequestMapping("/updateeducate")
	public String updateeducate(HttpServletRequest req,HttpServletResponse resp) {
		String idsrc = req.getParameter("id");
		int id = Integer.parseInt(idsrc);
		Educate educate = educateService.detailPlaneducate(id);
		req.setAttribute("educate", educate);
		return "updateeducate";
		
	}
	
	/*
	 * @RequestMapping("/updateeducate") public String
	 * updateeducate(HttpServletRequest req,HttpServletResponse resp) { String idsrc
	 * = req.getParameter("id"); int id = Integer.parseInt(idsrc); int re =
	 * educateService.updateeducate(id); if(re==1) { return list(req,resp); }else {
	 * return "error"; } }
	 */
}

